package ${packgeDO.serviceImplPackageName};

import java.util.List;

<#if bean_is_auto_wire?string=="1">
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
</#if>
<#assign len = domain.className?index_of("DO")>
<#assign DAOName = "${domain.className?substring(0,len)}Mapper">
<#assign lower_dao_name = "${domain.className?substring(0,len)?uncap_first}Mapper">
<#assign param="${domain.className?uncap_first}">
import lombok.extern.slf4j.Slf4j;
import ${packgeDO.daoPackageName}.${DAOName};
import ${domain.packageName}.${domain.className};
import ${packgeDO.servicePackageName}.${domain.className?substring(0,len)}Service;
import ${packgeDO.utilPackageName}.Page;
import ${packgeDO.exceptionPackageName}.${domain.exceptionName};

<#if bean_is_auto_wire?string=="1">
@Service(value="${domain.className?substring(0,len)?uncap_first}Service")
</#if>
@Slf4j
public class ${domain.className?substring(0,len)}ServiceImpl  implements ${domain.className?substring(0,len)}Service{

	<#if bean_is_auto_wire?string=="1">@Autowired</#if>
	private ${DAOName} ${lower_dao_name};

	<#if bean_is_auto_wire?string=="0">
	public void set${DAOName}(${DAOName} ${lower_dao_name}) {
		this.${lower_dao_name} = ${lower_dao_name};
	}

	</#if>
	
	@Override
	public int save(${domain.className} ${param}) throws ${domain.exceptionName} {
		try {
			return ${lower_dao_name}.insert(${param});
		} catch(Exception e){
			log.error(e.getMessage(),e);
			throw new ${domain.exceptionName}(e);
		}
	}

	@Override
	public int updateDynamic(${domain.className} ${param}) throws ${domain.exceptionName} {
		try {
			return ${lower_dao_name}.updateDynamic(${param});
		} catch(Exception e){
			log.error(e.getMessage(),e);
			throw new ${domain.exceptionName}(e);
		}
	}

	@Override
	public int deleteById(Long id) throws ${domain.exceptionName} {
		try {
			return (Integer) ${lower_dao_name}.deleteByPrimaryKey(id);
		} catch(Exception e){
			log.error(e.getMessage(),e);
			throw new ${domain.exceptionName}(e);
		}
	}

	@Override
	public ${domain.className} selectById(Long id) throws ${domain.exceptionName} {
		try {
			return ${lower_dao_name}.selectByPrimaryKey(id);
		} catch(Exception e){
			log.error(e.getMessage(),e);
			throw new ${domain.exceptionName}(e);
		}
	}

	@Override
	public Long selectCountDynamic(${domain.className} ${param}) throws ${domain.exceptionName} {
		try {
			return ${lower_dao_name}.selectCountDynamic(${param});
		} catch(Exception e){
			log.error(e.getMessage(),e);
			throw new ${domain.exceptionName}(e);
		}
	}

	@Override
	public List<${domain.className}> selectDynamic(${domain.className} ${param}) throws ${domain.exceptionName} {
		try {
			return ${lower_dao_name}.selectDynamic(${param});
		} catch(Exception e){
			log.error(e.getMessage(),e);
			throw new ${domain.exceptionName}(e);
		}
	}
	

	private List<${domain.className}> selectDynamicPageQuery(${domain.className} ${param}) throws ${domain.exceptionName} {
		try {
			return ${lower_dao_name}.selectDynamicPageQuery(${param});
		} catch(Exception e){
			log.error(e.getMessage(),e);
			throw new ${domain.exceptionName}(e);
		}
	}

	@Override
	public Page<${domain.className}> pageQuery(${domain.className} ${param}) throws ${domain.exceptionName} {
	 	try {
			if (${param} != null) {
				Long totalCount = this.selectCountDynamic(${param});
				List<${domain.className}> resultList = this.selectDynamicPageQuery(${param});

				Page<${domain.className}> page = new Page<${domain.className}>();
				page.setPageNo(${param}.getPageNo());
				page.setPageSize(${param}.getPageSize());
				page.setTotalCount(totalCount.intValue());
				page.setList(resultList);
				return page;
			}
		}  catch(Exception e){
			log.error(e.getMessage(),e);
			throw new ${domain.exceptionName}(e);
		}
		return new Page<${domain.className}>();
	}

	@Override
	public Page<${domain.className}> pageQueryByPageNo(${domain.className} ${param},int pageNo,int pageSize) throws ${domain.exceptionName} {
		try {
			if (${param} != null && pageNo>0 && pageSize>0) {
				${param}.setPageNo(pageNo);
				${param}.setPageSize(pageSize);
				return this.pageQuery(${param});
			}
		} catch(Exception e){
			log.error(e.getMessage(),e);
			throw new ${domain.exceptionName}(e);
		}
		return new Page<${domain.className}>();
	}

	@Override
	public void saveForBatch(List<${domain.className}> list) throws ${domain.exceptionName} {
		try {
			${lower_dao_name}.saveForBatch(list);
		} catch(Exception e){
			log.error(e.getMessage(),e);
			throw new ${domain.exceptionName}(e);
		}
	}
	
	@Override
	public void updateForBatch(List<${domain.className}> list) throws ${domain.exceptionName} {
		try {
			${lower_dao_name}.updateForBatch(list);
		} catch(Exception e){
			log.error(e.getMessage(),e);
			throw new ${domain.exceptionName}(e);
		}
	}
	
	@Override
	public ${domain.className} selectOne(${domain.className} ${param}) throws ${domain.exceptionName} {
		try {
			return ${lower_dao_name}.selectOne(${param});
		} catch(Exception e){
			log.error(e.getMessage(),e);
			throw new ${domain.exceptionName}(e);
		}
	}

}
